package no.ntnu.item.ttm4115.termproject.imagent;

import no.ntnu.item.ttm4115.termproject.InitiateIMCounterpart;
import no.ntnu.item.ttm4115.termproject.imagent.gui.IMWindow;
import no.tellu.common.javaframe.messages.RoleRequestMsg;
import no.tellu.common.actorframe.messages.RoleConfirmMsg;
import no.ntnu.item.ttm4115.termproject.InstantMessage;
import no.tellu.common.actorframe.messages.RoleDeniedMsg;
import no.ntnu.item.ttm4115.termproject.StartIMCounterpart;
import no.ntnu.item.ttm4115.termproject.ImEstablishedToPa;
import no.ntnu.item.ttm4115.termproject.sendMyFrameToIM;
import no.ntnu.item.ttm4115.termproject.ClosingChat;


public class IMAgentActions extends Object {

	
	public static void roleRequestToCouterpart(InitiateIMCounterpart signal, IMAgentSM asm){
	    
		asm.myName = signal.myName;
		asm.recevierName = signal.recevierName;
		System.out.println("receiver address er: "+signal.recevierAddress);
		asm.mainFrame = signal.mainFrame;
		System.out.println("sending role request to: "+signal.recevierAddress);
		
		//asm.im = new IMWindow(asm, signal.recevierAddress, signal.myName, signal.recevierName, signal.mainFrame);
		//asm.im.systemOkMsg("Requesting to connect with "+signal.recevierName+"...");
		
		asm.sendMessage(new RoleRequestMsg(signal.myName+"/imAgent", "IMAgent"), signal.recevierAddress);
		System.out.println("sendte role request");
	}

	public static void publishMessage(InstantMessage signal, IMAgentSM asm){
		asm.im.receiveMsg(signal.message);
	}

	public static void closeIM(RoleDeniedMsg signal, IMAgentSM asm){
		asm.im.systemErrorMsg("Connection denied");
	}

	public static void startIM(RoleConfirmMsg signal, IMAgentSM asm){
		asm.im = new IMWindow(asm, signal.getSenderRole(), asm.myName, asm.recevierName, asm.mainFrame);
		asm.im.systemOkMsg("Connection established");
		asm.im.systemOkMsg("Welcome to TreasureHunt chat!");
		asm.im.receiveSelfMsg(" ");
		asm.sendMessage(new StartIMCounterpart(asm.recevierName, asm.myName), signal.getSenderRole());
	
	}

	public static void establishIM(StartIMCounterpart signal, IMAgentSM asm){
	    asm.senderRole = signal.getSenderRole();
	    asm.myName = signal.senderName;
	    asm.senderName = signal.receiverName;
	    
	    asm.sendMessage(new ImEstablishedToPa(asm.senderName), asm.getContext().getParentAddress());
	}

	public static void startupIM(sendMyFrameToIM signal, IMAgentSM asm){
	    
		asm.im = new IMWindow(asm, asm.senderRole, asm.myName, asm.senderName, signal.mainFrame);
		asm.im.systemOkMsg("Incoming chat request from "+asm.senderName);
		asm.im.systemOkMsg("Welcome to TreasureHunt chat!"+"\n");
		asm.im.receiveSelfMsg(" ");
	}

	public static void publishClose(ClosingChat signal, IMAgentSM asm){
	    // TODO edit action statements
		asm.im.closedChat();
	}



}
